/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.huikecloud.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.huikecloud.entity.Host;
import org.springblade.modules.huikecloud.entity.Port;
import org.springblade.modules.huikecloud.excel.HostExcel;
import org.springblade.modules.huikecloud.excel.HostImportListener;
import org.springblade.modules.huikecloud.service.IHostService;
import org.springblade.modules.huikecloud.vo.HostVO;
import org.springblade.modules.huikecloud.wrapper.HostWrapper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * 主机表 控制器
 *
 * @author Blade
 * @since 2021-07-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("mapping/host")
@Api(value = "主机表", tags = "主机表接口")
public class HostController extends BladeController {

	private IHostService hostService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入host")
	public R<HostVO> detail(Host host) {
		Host detail = hostService.getOne(Condition.getQueryWrapper(host));
		return R.data(HostWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 主机表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入host")
	public R<IPage<Host>> list(@ApiIgnore @RequestParam Map<String, Object> host, Query query) {

//		QueryWrapper<Host> queryWrapper = Condition.getQueryWrapper((Map<String, Object>) host, Host.class);
//		IPage<Host> pages = hostService.page(Condition.getPage(query), queryWrapper);
//		return R.data(pages);

		IPage<Host> pages = hostService.page(Condition.getPage(query), Condition.getQueryWrapper(host, Host.class));
		return R.data(pages);
//		return R.data(HostWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 主机表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入host")
	public R<IPage<HostVO>> page(HostVO host, Query query) {
		IPage<HostVO> pages = hostService.selectHostPage(Condition.getPage(query), host);
		return R.data(pages);
	}

	/**
	 * 新增 主机表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入host")
	public R save(@Valid @RequestBody Host host) {
		return R.status(hostService.save(host));
	}

	/**
	 * 修改 主机表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入host")
	public R update(@Valid @RequestBody Host host) {
		return R.status(hostService.updateById(host));
	}

	/**
	 * 新增或修改 主机表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入host")
	public R submit(@Valid @RequestBody Host host) {
		if (host.getId()==null){
			host.setTcpSize(0);
			host.setWebSize(0);
		}
		return R.status(hostService.submit(host));
	}

	
	/**
	 * 删除 主机表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(hostService.deleteLogic(Func.toLongList(ids)));
	}
	/**
	 * 导入主机
	 */
	@PostMapping("import-host")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "导入用户", notes = "传入excel")
	public R importHost(MultipartFile file, Integer isCovered) {
		String filename = file.getOriginalFilename();
		if (StringUtils.isEmpty(filename)) {
			throw new RuntimeException("请上传文件!");
		}
		if ((!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx"))) {
			throw new RuntimeException("请上传正确的excel文件!");
		}
		InputStream inputStream;
		try {
			HostImportListener importListener = new HostImportListener(hostService);
			inputStream = new BufferedInputStream(file.getInputStream());
			ExcelReaderBuilder builder = EasyExcel.read(inputStream, HostExcel.class, importListener);
			builder.doReadAll();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.success("操作成功");
	}
	
}
